﻿using AutoMapper;
using MediatR;
using SC.GirdManage.Common;
using SC.GirdManage.Common.Enums;
using SC.GirdManage.Repository;
using SC.GridManage.API.Read.Application.command.LogicGrid;
using SC.GridManage.API.Read.Extensions;
using SC.GridManage.Domain.Models;

namespace SC.GridManage.API.Read.Application.handler.LogicGrid
{
    public class GetLogicGridHandler : IRequestHandler<GetLogicGridListCommand, ApiResult<List<GetLogicGridDTO>>>
    {
        private readonly IBaseRepository<LogicGridModel> logicGridRepo;
        private readonly IBaseRepository<DepartmentsModel> departRepo;
        private readonly IMapper mapper;

        public GetLogicGridHandler(IBaseRepository<LogicGridModel> logicGridRepo, IBaseRepository<DepartmentsModel> departRepo, IMapper mapper)
        {
            this.logicGridRepo = logicGridRepo;
            this.departRepo = departRepo;
            this.mapper = mapper;
        }

        /// <summary>
        /// 三表联查,逻辑网格 父级逻辑网格  部门
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<List<GetLogicGridDTO>>> Handle(GetLogicGridListCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<GetLogicGridDTO>> result = new ApiResult<List<GetLogicGridDTO>>();

            //查询的列表-逻辑网格
            var logicGrids = logicGridRepo.QueryAsync().Result.Where(x => x.IsDeleted == false);
            if (!request.GridName.IsNullOrEmpty()) {
                logicGrids = logicGrids.Where(x=>x.GridName.Contains(request.GridName));
            }


            //是父级逻辑网格
            var logicPrent = logicGridRepo.QueryAsync().Result.Where(x => x.IsDeleted == false);
            if (!request.GridParentName.IsNullOrEmpty()) {
                logicPrent = logicPrent.Where(x=>x.GridName.Contains(request.DepartmentName));
            }

            //因为要展示部门名称,所以联查了部门表
            var departMents = departRepo.QueryAsync().Result.Where(x => x.IsDeleted == false);
            if (!request.DepartmentName.IsNullOrEmpty()) {
                departMents = departMents.Where(x=>x.DepartmentName.Contains(request.DepartmentName));
            }

            var linq = from l in logicGrids                      
                       join d in departMents on l.DepartmentId equals d.Id  
                       join p in logicPrent on l.ParentGridId equals p.Id  
                       select new GetLogicGridDTO { 
                            Id = l.Id,
                            DepartmentId = l.DepartmentId,
                            DepartmentName = d.DepartmentName,
                            ParentGridId=p.Id,
                            ParentGridName = p.GridName,
                            GridName= l.GridName,
                            GridNo=l.GridNo
                       };

            result.Data=linq.ToList();
            result.Code =EnumResultCode.Success;
            result.Msg = "查询逻辑网格列表成功!";

            return Task.FromResult(result);
        }
    }
}
